package com.huike.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.pojo.Drug;
import com.huike.service.DrugService;
import com.huike.vo.ExportExcel;
import com.huike.vo.QiniuUtils;
import com.huike.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController()
@RequestMapping("drug")
public class DrugController {

    private static final String PATH = "http://r2c7vnn3n.hn-bkt.clouddn.com/";

    @Autowired
    private DrugService drugService;

    @RequestMapping("getDrugs")
    public List<Drug> getDrugs(){
        return drugService.getDrugs("","");
    }

    @RequestMapping("excelList")
    public ResultVO<Drug> excelList(){
        List<Drug> list = drugService.getDrugs("","");
        return new ResultVO<>(list);
    }

    @RequestMapping("list")
    public ResultVO<Drug> getDrugs(String drugName, String drugType,
                                   @RequestParam(value="pageNum",defaultValue="1") Integer pageNum,
                                   @RequestParam(value = "pageSize" ,defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Drug> list = drugService.getDrugs(drugName,drugType);
        PageInfo<Drug> pageInfo= new PageInfo<>(list,5);
        return new ResultVO<>(pageInfo);
    }

    @RequestMapping("addDrug")
    public ResultVO<Void> addDrug(@RequestParam(value="image",required=false) MultipartFile multipartFilest,
                                  Double inPrice,Double outPrice,String drugName, String drugType,
                                  String description, Integer quality, String detailDescription,
                                  String factory,String instruction,String note){
        String img = "";
        if(multipartFilest.getSize()!=0){
            String code = UUID.randomUUID().toString();
            //获取后缀
            String filename = multipartFilest.getOriginalFilename();
            String substring = filename.substring(filename.lastIndexOf("."));
            QiniuUtils upload = new QiniuUtils();
            try{
                upload.upload2Qiniu(multipartFilest.getBytes(),code+substring);
                img=(PATH+code+substring);
            }catch (Exception e){
                return new ResultVO<>(400,"递交图片到服务器时有错误");
            }
        }
        Drug drug = new Drug(img,inPrice,outPrice, drugName,drugType,
                description,quality,detailDescription,factory,instruction,note);
        Integer row = drugService.addDrug(drug);
        if(row<=0){
            return new ResultVO<>(500,"添加时未知异常");
        }
        return new ResultVO<>(200,"添加成功");
    }

    @RequestMapping("getDrupById")
    public Drug getDrupById(Integer id){
        return drugService.getDrugsById(id);
    }

    @RequestMapping("addResetNumber")
    public ResultVO<Void> addResetNumber(Integer id,Integer increase){
        Integer row = drugService.addResetNumber(id,increase);
        if(row>=1){
            return new ResultVO<>(200,"添加成功");
        }
        return new ResultVO<>(500,"添加失败");
    }

    @RequestMapping("updateLogo")
    public ResultVO<Void>updateLogo(Integer id,String oldImgName,@RequestParam("image") MultipartFile multipartFilest){

        String code = UUID.randomUUID().toString();
        //获取后缀
        String filename = multipartFilest.getOriginalFilename();
        String substring = filename.substring(filename.lastIndexOf("."));
        String img = "";
        QiniuUtils upload = new QiniuUtils();
        String oldName= oldImgName.substring(oldImgName.lastIndexOf("/")+1);
        upload.deleteFileFromQiniu(oldName);
        try{
            upload.upload2Qiniu(multipartFilest.getBytes(),code+substring);
            img=(PATH+code+substring);
        }catch (Exception e){
            return new ResultVO<>(400,"递交图片到服务器时有错误");
        }
        Integer row = drugService.updateLogo(id,img);
        if(row>=1){
            return new ResultVO<>(200,"修改成功");
        }
        return new ResultVO<>(500,"修改失败");
    }

    @RequestMapping("updateDrug")
    public ResultVO<Void>updateDrug(Drug drug){
        Integer row = drugService.updateDrug(drug);
        if(row>=1){
            return new ResultVO<>(200,"修改成功");
        }
        return new ResultVO<>(500,"修改失败");
    }

    @RequestMapping("excelInfo")
    public ResultVO<Void> excelInfo(String outStream){
        List<Drug> list = drugService.getDrugs("","");
        String[] headerStr = {"药品编号","药品图片","进价","售价","药品名称", "简单描述",
                "药品类型","保质期","详细描述","供应商","服用说明","备注",
                "总进数量","发药数量","库存","状态"};
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fileName=outStream +"\\"+ sdf.format(new Date())+"药品表.xls";
        ExportExcel exportExcel = new ExportExcel();
        try{
            OutputStream outputStream = new FileOutputStream(fileName);
            exportExcel.exportExcel(list,outputStream,headerStr);
        }catch (Exception e){
            return new ResultVO<>(500,"导出时未知异常");
        }
        return new ResultVO<>(200,"导出成功");
    }

}
